using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 诅咒Buff - 降低目标所有属性
/// </summary>
public class CurseBuff : BuffBase
{
    private Dictionary<AttributeType, float> m_OriginalAttributes;
    private Dictionary<AttributeType, float> m_AttributeReductions;

    public CurseBuff(BuffData buffData, Entity caster, Entity target) : base(buffData, caster, target)
    {
        m_OriginalAttributes = new Dictionary<AttributeType, float>();
        m_AttributeReductions = new Dictionary<AttributeType, float>();
        
        if (buffData is CurseData curseData)
        {
            foreach (var reduction in curseData.AttributeReductions)
            {
                m_AttributeReductions[reduction.AttributeType] = reduction.ReductionValue;
            }
        }
        else
        {
            // 默认诅咒效果
            m_AttributeReductions[AttributeType.MoveSpeed] = 0.2f; // 移动速度降低20%
            m_AttributeReductions[AttributeType.AttackPower] = 0.15f; // 攻击力降低15%
            m_AttributeReductions[AttributeType.Defense] = 0.1f; // 防御力降低10%
        }
    }

    protected override void OnStart()
    {
        base.OnStart();
        
        // 保存原始属性值
        SaveOriginalAttributes();
        
        // 应用诅咒效果
        ApplyCurseEffects();
        
        // 播放诅咒特效
        if (BuffData.Effect != null)
        {
            var effect = GameObject.Instantiate(BuffData.Effect, Target.transform);
            effect.transform.localPosition = Vector3.zero;
        }
        
        Logger.Log("CurseBuff", $"{Target.name} 被诅咒了，所有属性降低");
    }

    protected override void OnTick(float dt)
    {
        base.OnTick(dt);
        // 诅咒期间不需要特殊处理
    }

    private void SaveOriginalAttributes()
    {
        // 保存移动速度
        if (Target.MoveComponent != null)
        {
            m_OriginalAttributes[AttributeType.MoveSpeed] = Target.MoveComponent.Speed;
        }
        
        // 保存其他属性（需要根据AttrComponent的具体实现）
        // if (Target.AttrComponent != null)
        // {
        //     m_OriginalAttributes[AttributeType.AttackPower] = Target.AttrComponent.AttackPower;
        //     m_OriginalAttributes[AttributeType.Defense] = Target.AttrComponent.Defense;
        //     // 保存其他属性...
        // }
    }

    private void ApplyCurseEffects()
    {
        // 应用移动速度诅咒
        if (m_AttributeReductions.ContainsKey(AttributeType.MoveSpeed) && Target.MoveComponent != null)
        {
            float originalSpeed = m_OriginalAttributes[AttributeType.MoveSpeed];
            float reduction = m_AttributeReductions[AttributeType.MoveSpeed];
            Target.MoveComponent.SetSpeed(originalSpeed * (1.0f - reduction));
        }
        
        // 应用其他属性诅咒（需要根据AttrComponent的具体实现）
        // if (Target.AttrComponent != null)
        // {
        //     foreach (var reduction in m_AttributeReductions)
        //     {
        //         if (reduction.Key != AttributeType.MoveSpeed)
        //         {
        //             float originalValue = m_OriginalAttributes[reduction.Key];
        //             float reductionValue = reduction.Value;
        //             // 应用诅咒效果...
        //         }
        //     }
        // }
    }

    private void RestoreOriginalAttributes()
    {
        // 恢复移动速度
        if (m_OriginalAttributes.ContainsKey(AttributeType.MoveSpeed) && Target.MoveComponent != null)
        {
            Target.MoveComponent.SetSpeed(m_OriginalAttributes[AttributeType.MoveSpeed]);
        }
        
        // 恢复其他属性（需要根据AttrComponent的具体实现）
        // if (Target.AttrComponent != null)
        // {
        //     foreach (var original in m_OriginalAttributes)
        //     {
        //         if (original.Key != AttributeType.MoveSpeed)
        //         {
        //             // 恢复原始属性值...
        //         }
        //     }
        // }
    }

    protected override void OnFinish()
    {
        base.OnFinish();
        Logger.Log("CurseBuff", $"{Target.name} 诅咒效果结束");
    }

    protected override void OnRemove()
    {
        base.OnRemove();
        
        // 恢复原始属性值
        RestoreOriginalAttributes();
        
        // 清理特效
        var effects = Target.GetComponentsInChildren<ParticleSystem>();
        foreach (var effect in effects)
        {
            if (effect.name.Contains("Curse"))
            {
                GameObject.Destroy(effect.gameObject);
            }
        }
    }

    public override bool IsCanMerge()
    {
        return true; // 诅咒效果可以叠加，刷新持续时间
    }
}